//
//  MyActivityListViewController.m
//  app001
//
//  Created by xionghao on 15/10/9.
//  Copyright © 2015年 xionghao. All rights reserved.
//

#import "MyActivityListViewController.h"
#import "HYSegmentedControl.h"
#import "PublishedTableViewController.h"
#import "NotPubTableViewController.h"
#import "BottomBar.h"
@interface MyActivityListViewController ()<HYSegmentedControlDelegate,UIScrollViewDelegate,PublishedAGControllerDelegate,NotPublishedAGControllerDelegate>{
    BOOL _isedting;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) HYSegmentedControl *hysegment;
@property (nonatomic,strong) UIScrollView *scrollview;
@property (nonatomic,strong) BottomBar *firstbar;
@property (nonatomic,strong) BottomBar *secondbar;
@property (nonatomic,strong) PublishedTableViewController *pvc;
@property (nonatomic,strong) NotPubTableViewController *npvc;
@end

@implementation MyActivityListViewController


-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.hysegment=[[HYSegmentedControl alloc]initWithOriginY:64 Titles:@[@"已发布",@"未发布"] delegate:self];
    [self.view addSubview:self.hysegment];
    self.navigationItem.title=self.isGet?@"纪念列表":@"活动列表";
    [self setupview];
    // Do any additional setup after loading the view.
}

-(void)downCompleteWithviewcontroller:(PublishedTableViewController *)cotroller{
    [self.npvc needupdate];
}
-(void)UPCompleteWithviewcontroller:(NotPubTableViewController *)cotroller{
    [self.pvc needupdate];
}
-(void)setupvc{
    PublishedTableViewController *pvc=[[PublishedTableViewController alloc] initWithStyle:UITableViewStylePlain];
    pvc.view.frame=self.scrollview.bounds;
    pvc.delegate=self;
    self.pvc=pvc;
    pvc.isEidt=_isedting;
    pvc.isGet=self.isGet;
    [self.scrollview addSubview:pvc.view];
    [self addChildViewController:pvc];
    
    NotPubTableViewController *npvc=[[NotPubTableViewController alloc] initWithStyle:UITableViewStylePlain];
    npvc.view.frame=self.scrollview.bounds;
    npvc.delegate=self;
    npvc.isGet=self.isGet;
    npvc.isEidt=_isedting;
    npvc.view.x=XHScreenwidth;
    self.npvc=npvc;
    [self.scrollview addSubview:npvc.view];
    [self addChildViewController:npvc];
    BottomBar *bar=[[BottomBar alloc]initWithBtnTitleArray:@[@"删除",@"取消发布"] WithDelegate:pvc:YES];
    bar.posttile=self.isGet?@"添加纪念":@"添加活动";
    bar.datasource=pvc;
    [bar showInView:self.scrollview];
    
    self.firstbar=bar;
    
    pvc.bottombar=self.firstbar;
 
    BottomBar *downbar=[[BottomBar alloc]initWithBtnTitleArray:@[@"删除",@"发布"] WithDelegate:npvc :YES];
    downbar.posttile=self.isGet?@"添加纪念":@"添加活动";
    downbar.x=XHScreenwidth;
    [downbar showInView:self.scrollview];
    downbar.datasource=npvc;
    self.secondbar=downbar;
  
  
    npvc.bottombar=self.secondbar;
}




-(void)setupRight:(BOOL)iseidting{
    
    NSMutableDictionary *userinfo=[NSMutableDictionary dictionary];
    if (iseidting) {
        UIBarButtonItem *rightitem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(rightclick)];
        
        self.navigationItem.rightBarButtonItem=rightitem;
        userinfo[@"iseiting"]=@(1);
    }else{
        self.firstbar.btnenable=NO;
        self.secondbar.btnenable=NO;
        UIBarButtonItem *rightitem=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(rightclick)];
        userinfo[@"iseiting"]=@(0);
        self.navigationItem.rightBarButtonItem=rightitem;
    }
    NSNotification *notice=[[NSNotification alloc]initWithName:NoticeIsEidt object:nil userInfo:userinfo];
    [[NSNotificationCenter defaultCenter] postNotification:notice];
    
}
-(void)rightclick{
    _isedting=!_isedting;
    self.pvc.isEidt=_isedting;
    [self.pvc.tableView reloadData];
    self.npvc.isEidt=_isedting;
    [self.npvc.tableView reloadData];
    [self setupRight:_isedting];
    [self.firstbar switchView];
    [self.secondbar switchView];
    
}
-(void)setupview{
    //初始化导航栏右侧按钮
    [self setupRight:NO];
    //初始化scrollview
    UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, XHScreenwidth, XHScreenheight-100)];
    
    scrollview.backgroundColor=[UIColor redColor];
    scrollview.contentSize=CGSizeMake(XHScreenwidth*2, XHScreenheight-100);
    [self.view addSubview:scrollview];
    
    self.scrollview=scrollview;
    self.scrollview.delegate=self;
    self.scrollview.pagingEnabled=YES;
    self.scrollview.showsHorizontalScrollIndicator=NO;
    self.scrollview.showsVerticalScrollIndicator=NO;
    self.scrollview.bounces=NO;
    [self setupvc];
    
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat x=scrollView.contentOffset.x;
    NSLog(@"%f",x);
    if (x/XHScreenwidth==1) {
        [self.hysegment changeSegmentedControlWithIndex:1];
        
    }else{
        [self.hysegment changeSegmentedControlWithIndex:0];
        
    }
}

-(void)hySegmentedControlSelectAtIndex:(NSInteger)index{
    
    [self.scrollview setContentOffset:CGPointMake(XHScreenwidth*index, 0) animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
